#pragma once

#include <string>

#ifdef UNICODE
    typedef std::basic_string<wchar_t> tstring;
#else
    typedef std::string tstring;
#endif

class FileIterator
{
public:
    FileIterator(const tstring& dirName, const tstring& fileName)
        : hFind(INVALID_HANDLE_VALUE)
    {
        tstring rootName = dirName[dirName.size()-1] == _T('\\') ? dirName : dirName + _T('\\');
        filesName = rootName + fileName;

        Reset();
    }

    ~FileIterator()
    {
        if (hFind != INVALID_HANDLE_VALUE)
            FindClose(hFind);
    }

    void Reset()
    {
        if (hFind != INVALID_HANDLE_VALUE)
            FindClose(hFind);

        memset((LPVOID)&findData, 0, sizeof(WIN32_FIND_DATA));
        hFind = FindFirstFile(filesName.c_str(), &findData);
        
        if (hFind == INVALID_HANDLE_VALUE)
            throw std::exception();
    }

    const tstring Get() const
    {
        return findData.cFileName;
    }

    bool Next()
    {
        if (!FindNextFile(hFind, &findData)) {
            DWORD dwError = GetLastError();
            FindClose(hFind);
            hFind = INVALID_HANDLE_VALUE;
            return false;
        }

        return true;
    }

private:

    tstring filesName;

    WIN32_FIND_DATA findData;
    HANDLE hFind;
};